package com.lsfb.signln.Statistics.LeaveNum;

import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.ajguan.library.EasyRefreshLayout;
import com.lsfb.signln.Leave.LeaveListAdapter;
import com.lsfb.signln.Leave.LeaveListBean;
import com.lsfb.signln.Leave.PersonnelSelect.PersonnelSelectActivity;
import com.lsfb.signln.Location.TaskMsg.Leave.LeaveActivity;
import com.lsfb.signln.Login.LoginActivity;
import com.lsfb.signln.R;
import com.lsfb.signln.Utils.LittleUtils;
import com.lsfb.signln.Utils.MyProgressDialog;
import com.lsfb.signln.Utils.PopWindowManager;
import com.lsfb.signln.Utils.URLString;
import com.lsfb.signln.View.SignLoadview;
import com.lsfb.signln.View.SignRefreshHeadView;
import com.zgscwjm.lsfbbasetemplate.Event.EventAnnotation;
import com.zgscwjm.lsfbbasetemplate.LsfbActivity;
import com.zgscwjm.lsfbbasetemplate.annotation.ContentView;
import com.zgscwjm.lsfbbasetemplate.annotation.OnClick;
import com.zgscwjm.lsfbbasetemplate.annotation.ViewInject;
import com.zgscwjm.lsfbbasetemplate.internet.BaseInternet;
import com.zgscwjm.lsfbbasetemplate.utils.T;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * 请假次数
 * Created by Administrator on 2018/1/30 0030.
 */
@ContentView(R.layout.aty_leave_num)
public class LeaveNumActivity extends LsfbActivity implements LeaveListAdapter.ItemListener {

    /**
     * PopWindow管理类
     */
    private PopWindowManager replay;

    @ViewInject(R.id.fg_leave_top)
    private LinearLayout fg_leave_top;

    /**
     * 请假状态
     */
    @ViewInject(R.id.fg_text_leave_state)
    private TextView text_leave_state;

    /**
     * 请假人
     */
    @ViewInject(R.id.fg_text_leave_creator)
    private TextView text_leave_creator;

    /**
     * 请假分类
     */
    @ViewInject(R.id.fg_text_leave_type)
    private TextView text_leave_type;

    /**
     * 请假列表
     */
    @ViewInject(R.id.rv_leavelist)
    private RecyclerView rv_leavelist;
    private List<LeaveListBean.ListBean> datas;
    private LeaveListAdapter adapter;

    /**
     * 暂无数据
     */
    @ViewInject(R.id.aty_statistics_leave_layout_null)
    private RelativeLayout layout_null;

    /**
     * 下拉刷新 上拉加载
     */
    @ViewInject(R.id.easylayout)
    private EasyRefreshLayout easylayout;

    private boolean IsRefresh = true;

    private MyProgressDialog dialog;

    //页数
    private int page = 0;
    //请假状态
    private int state = 0;
    //请假分类
    private int type = 0;
    //创建人类型
    private String usertype = "0";
    //创建人
    private String users = null;
    //创建人-部门
    private String depars = null;

    @Override
    public void initView() {
        LittleUtils.setsimpletitlebar(this, "免定位次数");

        dialog = new MyProgressDialog(this, "加载中......");

        datas = new ArrayList<>();
        adapter = new LeaveListAdapter(this, datas, R.layout.item_leave, this);
        rv_leavelist.setLayoutManager(new LinearLayoutManager(this));
        rv_leavelist.setAdapter(adapter);

        easylayout.setLoadMoreView(new SignLoadview(this));
        easylayout.setRefreshHeadView(new SignRefreshHeadView(this));
        easylayout.addEasyEvent(new EasyRefreshLayout.EasyEvent() {
            //加载更多
            @Override
            public void onLoadMore() {
                page++;
                IsRefresh = false;
                getData();
            }

            //刷新
            @Override
            public void onRefreshing() {
                page = 0;
                IsRefresh = true;
                getData();
            }
        });
        getData();
    }

    @Override
    public void initEvent() {

    }

    @Override
    public void initData() {

    }

    /**
     * 点击事件
     *
     * @param view
     */
    @OnClick({R.id.fg_leave_state, R.id.fg_leave_creator, R.id.fg_leave_type})
    public void onClick(View view) {
        switch (view.getId()) {
            //请假状态
            case R.id.fg_leave_state:
                replay = new PopWindowManager(this, R.layout.pop_leave_state) {
                    @Override
                    public void onViewLister(View contentView, final PopWindowManager manager) {

                        final RelativeLayout RelativeLayout = (RelativeLayout) contentView.findViewById(R.id.RelativeLayout);
                        final TextView state_all = (TextView) contentView.findViewById(R.id.pop_leave_state_all);
                        final TextView wait_approve = (TextView) contentView.findViewById(R.id.pop_leave_state_wait_approve);
                        final TextView state_approve = (TextView) contentView.findViewById(R.id.pop_leave_state_approve);
                        final TextView state_refuse = (TextView) contentView.findViewById(R.id.pop_leave_state_refuse);
                        final TextView state_repeal = (TextView) contentView.findViewById(R.id.pop_leave_state_repeal);

                        RelativeLayout.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                manager.hideWindow();
                            }
                        });
                        //全部
                        state_all.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                text_leave_state.setText("全部");
                                state = 0;
                                page = 0;
                                IsRefresh = true;
                                getData();
                                manager.hideWindow();
                            }
                        });
                        //待审核
                        wait_approve.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                state = 1;
                                page = 0;
                                IsRefresh = true;
                                getData();
                                text_leave_state.setText("待审批");
                                manager.hideWindow();
                            }
                        });
                        //已审核
                        state_approve.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                state = 2;
                                page = 0;
                                IsRefresh = true;
                                getData();
                                text_leave_state.setText("已通过");
                                manager.hideWindow();
                            }
                        });
                        //已拒绝
                        state_refuse.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                state = 3;
                                page = 0;
                                IsRefresh = true;
                                getData();
                                text_leave_state.setText("已拒绝");
                                manager.hideWindow();
                            }
                        });
                        //已撤销
                        state_repeal.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                state = 4;
                                page = 0;
                                IsRefresh = true;
                                getData();
                                text_leave_state.setText("已撤销");
                                manager.hideWindow();
                            }
                        });
                    }
                };
                replay.showWindows(fg_leave_top, 0, 0);
                break;
            //创建者
            case R.id.fg_leave_creator:
                startActivityForResult(new Intent(this, PersonnelSelectActivity.class), 0);
                break;
            //请假分类
            case R.id.fg_leave_type:
                replay = new PopWindowManager(this, R.layout.pop_leave_type) {
                    @Override
                    public void onViewLister(View contentView, final PopWindowManager manager) {

                        final RelativeLayout RelativeLayout = (RelativeLayout) contentView.findViewById(R.id.RelativeLayout);
                        final TextView type_all = (TextView) contentView.findViewById(R.id.pop_leave_type_all);
                        final TextView my_approve = (TextView) contentView.findViewById(R.id.pop_leave_type_my_approve);
                        final TextView my_creator = (TextView) contentView.findViewById(R.id.pop_leave_type_my_creator);
                        final TextView my_copy = (TextView) contentView.findViewById(R.id.pop_leave_type_my_copy);

                        RelativeLayout.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                manager.hideWindow();
                            }
                        });
                        //全部
                        type_all.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                type = 0;
                                page = 0;
                                IsRefresh = true;
                                getData();
                                text_leave_type.setText("全部");
                                manager.hideWindow();
                            }
                        });
                        //由我审批的
                        my_approve.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                type = 1;
                                page = 0;
                                IsRefresh = true;
                                getData();
                                text_leave_type.setText("由我审批的");
                                manager.hideWindow();
                            }
                        });
                        //由我创建的
                        my_creator.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                type = 2;
                                page = 0;
                                IsRefresh = true;
                                getData();
                                text_leave_type.setText("由我创建的");
                                manager.hideWindow();
                            }
                        });
                        //抄送给我的
                        my_copy.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                type = 3;
                                page = 0;
                                IsRefresh = true;
                                getData();
                                text_leave_type.setText("抄送给我的");
                                manager.hideWindow();
                            }
                        });
                    }
                };
                replay.showWindows(fg_leave_top, 0, 0);
                break;
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (data == null) {
            return;
        } else {
            switch (requestCode) {
                case 0:
                    String type = data.getStringExtra("usertype");
                    text_leave_creator.setText(type);
                    if (type.equals("我自己")) {
                        usertype = "0";
                    } else if (type.equals("自选")) {
                        usertype = "1";
                    } else if (type.equals("全部")) {
                        usertype = "2";
                        if (data.getStringExtra("users") != null) {
                            users = data.getStringExtra("users");
                        } else {
                            users = null;
                        }
                        if (data.getStringExtra("depars") != null) {
                            depars = data.getStringExtra("depars");
                        } else {
                            depars = null;
                        }
                    }
                    getData();
                    break;
            }
        }
    }

    @Override
    public void Resume() {
        getData();
    }

    /**
     * 获取请假列表
     */
    private void getData() {
        HashMap hashMap = new HashMap();
        hashMap.put("state", String.valueOf(state));
        hashMap.put("type", String.valueOf(type));
        hashMap.put("usertype", String.valueOf(usertype));
        if (usertype.equals("1")) {
            if (users != null) {
                hashMap.put("users", users);
            }
            if (depars != null) {
                hashMap.put("depars", depars);
            }
        }
        hashMap.put("page", String.valueOf(page));
        dialog.showdialog();
        new BaseInternet().getData(URLString.LEAVELIST, hashMap, LeaveListBean.class, new LeaveCountEvent<LeaveListBean>(), true);
    }

    @EventAnnotation
    public void eventResult(LeaveCountEvent<LeaveListBean> event) {
        dialog.hidedialog();
        easylayout.refreshComplete();
        easylayout.loadMoreComplete();
        switch (event.getCode()) {
            case 200:
                if (event.getData().getList() != null && !event.getData().getList().isEmpty()) {
                    if (IsRefresh) {
                        datas.clear();
                        rv_leavelist.onScrolled(0, 0);
                    }
                    datas.addAll(event.getData().getList());
                    layout_null.setVisibility(View.GONE);
                } else {
                    if (IsRefresh) {
                        datas.clear();
                        layout_null.setVisibility(View.VISIBLE);
                    } else {
                        page--;
                        T.showShort(this, "没有更多数据");
                        layout_null.setVisibility(View.GONE);
                    }
                }
                adapter.notifyDataSetChanged();
                break;
            case 1006:
                T.showShort(this, "您的账号已在其它设备登录，请重登录");
                LittleUtils.exit(this);
                startActivity(new Intent(this, LoginActivity.class));
                break;
            default:
                T.showShort(this, event.getMsg());
                break;
        }
    }

    @Override
    public void onItem(LeaveListBean.ListBean model) {
        //跳转请假申请详情
        startActivity(new Intent(this, LeaveActivity.class).putExtra("lid", String.valueOf(model.getId())));
    }
}
